/*
 * @Author: xiaosihan 
 * @Date: 2023-02-06 15:22:57 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-06-11 21:04:37
 */
import { AmbientLight, DirectionalLight, Vector3 } from "three";
import ThreeBase from "three-base";
import threeUtils from "three-base/threeUtils";
import keyContorls from "three-base/keyContorls";
import PointLight from "three-base/PointLight";

import Room from "./component3d/Room/Room";
import TV from "./component3d/TV/TV";
import { round } from "lodash";

import { degToRad } from "three/src/math/MathUtils";
import { autorun } from "mobx";
import homeStore from "./homeStore";


/**
 *
 *
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {

    constructor() {
        super();
    }

    // 环境光
    ambientLight = (() => {
        const ambientLight = new AmbientLight("#fff", 1.5);
        this.scene.add(ambientLight);
        return ambientLight;
    })();

    //点光源
    pointLight = (() => {
        const pointLight = new PointLight("#fff", 2);
        pointLight.distance = 10000;
        pointLight.castShadow = true;
        pointLight.position.set(500, 2000, 500);
        this.scene.add(pointLight);
        return pointLight;
    })();

    // 方向光
    directionalLight = (() => {
        const directionalLight = new DirectionalLight("#fff", 2);
        directionalLight.position.set(-10, 10, 5);
        directionalLight.target.position.set(0, 0, 0);
        directionalLight.castShadow = true;
        directionalLight.shadow.mapSize.set(4096, 4096);
        directionalLight.shadow.camera.left = -1000;
        directionalLight.shadow.camera.right = 1000;
        directionalLight.shadow.camera.top = 1000;
        directionalLight.shadow.camera.bottom = -1000;

        // this.scene.add(directionalLight);
        return directionalLight;
    })();

    init() {
        super.init();
        this.getCamera().far = 50000;
        this.renderer.shadowMap.enabled = true;

        // 开发模式时还原记录的位置
        if (threeUtils.isDev && localStorage.lookAt) {
            this.resetLookAt({
                ...JSON.parse(localStorage.lookAt)
            });
        }

        // 开发模式时记录当前的位置
        if (threeUtils.isDev) {
            this.controls.addEventListener("change", () => {
                localStorage.lookAt = JSON.stringify(this.getLookAt());
            })
        }

        if (threeUtils.isProd || (threeUtils.isDev && !localStorage.lookAt)) {
            this.resetLookAt({
                center: new Vector3(0, 0, 0),
                position: new Vector3(-7.049, 27.469, 59.276)
            });
        }

    }

    // 房间
    room = (() => {
        const room = new Room();
        this.scene.add(room);
        return room;
    })();

    //电视
    tv = (() => {
        const tv = new TV();
        tv.rotation.set(0, degToRad(-90), 0);
        tv.position.set(4100, 800, 0);
        keyContorls.addEventListener("change", (e: any) => {
            tv.position.add(e.direction);
            console.log(tv.position.toArray().map(v => round(v, 3)).join(", "));
        });
        this.scene.add(tv);
        return tv;
    })();

    dispose = autorun(() => {
        this.room.visible = homeStore.showRoom;
        this.tv.visible = homeStore.showTV;
    });

}


const homeRenderer = window.homeRenderer = new HomeRender();

export default homeRenderer;